package com.welike.shibo.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @author fangyu
 * @version v1.0.0
 * @since 2019/11/5 10:05 下午
 */
@Slf4j
public class MergeVideoMp3 {

    private String ffmpegPath;

    public MergeVideoMp3(String ffmpegPath) {
        this.ffmpegPath = ffmpegPath;
    }

    public String converetor(String videoInputPath, String mp3InputPath, double seconds, String videoOutputPath) throws IOException{
        // $ ffmpeg -i input.mp4 output.avi
        // ffmpeg -i a.mp4 -i b.mp3 -t 7 -c:v copy -c:a aac -strict experimental -map 0:v:0 -map 1:a:0 output1.mp4
        log.info("compound:input => {}, output => {}", videoInputPath, videoOutputPath);
        List<String> command = new ArrayList<>();
        command.add(ffmpegPath);
        command.add("-i");
        command.add(videoInputPath);

        command.add("-i");
        command.add(mp3InputPath);

        command.add("-t");
        command.add(String.valueOf(seconds/1000));

        command.add("-c:v");
        command.add("copy");

        command.add("-c:a");
        command.add("aac");

        command.add("-strict");
        command.add("experimental");

        command.add("-map");
        command.add("0:v:0");

        command.add("-map");
        command.add("1:a:0");

        command.add("-y");
        command.add(videoOutputPath);

        ProcessBuilder processBuilder = new ProcessBuilder(command);
        Process process = processBuilder.start();
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = bufferedReader.readLine()) != null){

        }
        if (bufferedReader != null){
            bufferedReader.close();
        }
        if (inputStreamReader != null){
            inputStreamReader.close();
        }
        if (errorStream != null){
            errorStream.close();
        }
        return videoOutputPath;
    }

    public Boolean screenshot(String videoInputPath, String imgOutputPath) throws IOException {
        // ffmpeg -ss 00:00:01 -i 201911171573977213547.mp4 -frames:v 1 -f image2 -y 222.jpg
        log.info("screenshot:input => {}, output => {}", videoInputPath, imgOutputPath);
        List<String> command = new ArrayList<>();

        command.add(ffmpegPath);
        command.add("-i");
        command.add(videoInputPath);

        command.add("-ss");
        command.add("1");

        command.add("-frames:v");
        command.add("1");

        command.add("-f");
        command.add("image2");

        command.add("-y");
        command.add(imgOutputPath);

        try {
            ProcessBuilder builder = new ProcessBuilder();
            builder.command(command);
            builder.start();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static void main(String[] args) {
        MergeVideoMp3 ffmpeg = new MergeVideoMp3("ffmpeg");
        try {
            ffmpeg.converetor("/Users/fangyu/Downloads/a.mp4", "/Users/fangyu/Downloads/b.mp3",
                    4, "/Users/fangyu/Downloads/output.mp4");
            ffmpeg.screenshot("/Users/fangyu/Downloads/a.mp4", "/Users/fangyu/Downloads/output.jpg");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
